﻿using ArcFaceSDK;
using ArcFaceSDK.Entity;
using ArcFaceSDK.SDKModels;
using ArcFaceSDK.Utils;
using ArcSoftFace.Utils;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestFace
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private FaceEngine imageEngine = new FaceEngine();
        public MainWindow()
        {
            InitializeComponent();
            InitEngines();

        }

        /// <summary>
        /// 初始化引擎
        /// </summary>
        private void InitEngines()
        {
            try
            {
                //读取配置文件
                AppSettingsReader reader = new AppSettingsReader();
                string appId = "G1UwLR6Jx2zLcmotC1P5GeoohZhoD9R5odesxzUwKYAP";

                string sdkKey32 = "8Mb1DvwC8YWhXLSRQBQmy6MKa85xaGzQvTtmpVxDFysN";
                
                //判断CPU位数
                var is64CPU = Environment.Is64BitProcess;

                //在线激活引擎    如出现错误，1.请先确认从官网下载的sdk库已放到对应的bin中，2.当前选择的CPU为x86或者x64
                int retCode = 0;

                retCode = imageEngine.ASFOnlineActivation(appId, sdkKey32);
                if (retCode != 0 && retCode != 90114)
                {
                    MessageBox.Show("激活SDK失败,错误码:" + retCode);
                    System.Environment.Exit(0);
                }



                //初始化引擎
                DetectionMode detectMode = DetectionMode.ASF_DETECT_MODE_IMAGE;
                //Video模式下检测脸部的角度优先值
                ASF_OrientPriority videoDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
                //Image模式下检测脸部的角度优先值
                ASF_OrientPriority imageDetectFaceOrientPriority = ASF_OrientPriority.ASF_OP_ALL_OUT;
                //人脸在图片中所占比例，如果需要调整检测人脸尺寸请修改此值，有效数值为2-32
                int detectFaceScaleVal = 16;
                //最大需要检测的人脸个数
                int detectFaceMaxNum = 15;
                //引擎初始化时需要初始化的检测功能组合
                int combinedMask = FaceEngineMask.ASF_FACE_DETECT | FaceEngineMask.ASF_FACERECOGNITION | FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER | FaceEngineMask.ASF_FACE3DANGLE;
                //初始化引擎，正常值为0，其他返回值请参考http://ai.arcsoft.com.cn/bbs/forum.php?mod=viewthread&tid=19&_dsign=dbad527e
                retCode = imageEngine.ASFInitEngine(detectMode, imageDetectFaceOrientPriority, detectFaceScaleVal, detectFaceMaxNum, combinedMask);
                Console.WriteLine("InitEngine Result:" + retCode);



                //初始化视频模式下人脸检测引擎
               
            }
            catch (Exception ex)
            {
                
            }
        }
        FaceFeature face1;
        private void f_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Multiselect = false;
            if (opf.ShowDialog() == true)
            {
                System.Drawing.Image image = ImageUtil.ReadFromFile(opf.FileName);

                if (image.Width % 4 != 0)
                {
                    image = ImageUtil.ScaleImage(image, image.Width - (image.Width % 4), image.Height);
                }

                int featureCode = -1;
                SingleFaceInfo singleFaceInfo = new SingleFaceInfo();

                face1 = FaceUtil.ExtractFeature(imageEngine, image, out singleFaceInfo, ref featureCode);

                MultiFaceInfo multiFaceInfo;
                int retCode = imageEngine.ASFDetectFacesEx(image, out multiFaceInfo);
            }
        }

        FaceFeature face2;
        private void s_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Multiselect = false;
            if (opf.ShowDialog() == true)
            {
                System.Drawing.Image image = ImageUtil.ReadFromFile(opf.FileName);

                if (image.Width % 4 != 0)
                {
                    image = ImageUtil.ScaleImage(image, image.Width - (image.Width % 4), image.Height);
                }

                int featureCode = -1;
                SingleFaceInfo singleFaceInfo = new SingleFaceInfo();

                face2 = FaceUtil.ExtractFeature(imageEngine, image, out singleFaceInfo, ref featureCode);

                MultiFaceInfo multiFaceInfo;
                int retCode = imageEngine.ASFDetectFacesEx(image, out multiFaceInfo);
            }
        }

        private void c_Click(object sender, RoutedEventArgs e)
        {
            float similarity = 0f;
           int cp= imageEngine.ASFFaceFeatureCompare(face1, face2, out similarity);

            if (cp == 0)
            {
                Console.WriteLine(similarity);
            }
        }
    }
}
